using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.Net;
using System.IO;
namespace ProcessesSMS
{
	/// <summary>
	/// Summary description for.
	/// Project name: SMS_xoso
	/// channel: n/a
	/// command code: HN X Y
	/// created: 01/09/09 .
	/// Writer : Nampt + Tuanbc
	/// Company: VIT TELECOM
	/// </summary>

	public class THOITIET
	{
		#region Declare
		private SMS_MO WMO;
		private SMS_MT WMT;
		ArrayList arr = new ArrayList();
		public THOITIET(SMS_MO MO)
		{
			WMO = MO;
		}

		public THOITIET()
		{
		}
		#endregion
        public void run()
        {
            WMT = new SMS_MT(WMO);
            string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
            tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
            string[] msg = tmpMsg.Split(new char[] { ' ' });
            string strReturnMsg = "";
            strReturnMsg = GetReturnMsg(msg);
            WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
            WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
            WMT.Insert();
            return;
        }
		public string GetReturnMsg(string[] arr)
		{			
			string msgBody="";
            if (WMO.ServiceID == "6072" || WMO.ServiceID == "6172")
            {
                return "De nhan thong tin thoi tiet, soan tin: TT TenTinh gui 6272.VD muon xem thoi tiet Ha Noi soan tin: TT HaNoi gui 6272";
            }
            if (arr.Length > 0)
            {
                msgBody = GetMsgStockFromURL("http://6x72.com.vn/SMSTT/?s=" + arr[0] + "");
            }
            else
            {
                msgBody = GetMsgStockFromURL("http://6x72.com.vn/SMSTT/?s=");
            }
			return msgBody;
		}
        private string GetMsgStockFromURL(string url)
        {
            string strval = "";
            WebRequest WQob = WebRequest.Create(url);
            WebResponse WPob = WQob.GetResponse();
            StreamReader SRob = new StreamReader(WPob.GetResponseStream());
            strval = SRob.ReadToEnd();
            WQob.Abort();
            return strval;
        }
	}		
}
